package cmp.compile.panels;

/***********************************************************
 * The "Setting" window for blast panel
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import util.methods.BlastArgs;
import util.ui.UserPrompt;
import cmp.main.Globals;

public class EditBlastPanel extends JPanel {
	private static final long serialVersionUID = -6189127919667748457L;
	private static final int HEADER_WIDTH = 120;

	public EditBlastPanel(BlastPanel parentPanel) {
		BlastArgs.availBlast();
		theParentPanel = parentPanel;
		
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setBackground(Globals.BGCOLOR);
		
		btnUseBlastOption = new JRadioButton("Use Blast File");
		btnUseBlastOption.setBackground(Globals.BGCOLOR);
		btnUseBlastOption.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updatePanel();
			}
		});
		btnGenBlastOption = new JRadioButton("Generate Blast File");
		btnGenBlastOption.setBackground(Globals.BGCOLOR);
		btnGenBlastOption.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updatePanel();
			}
		});

		ButtonGroup group = new ButtonGroup();
		
		group.add(btnUseBlastOption);
		group.add(btnGenBlastOption);

		// WN-note, panel is created before there is any knowledge of what the project is 
		// or what should go in this text field. Not ideal. 
		txtBlastFile = new FileSelectTextField(theParentPanel.getParentPanel(), 30, null,"blast"); 
		txtBlastFile.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				updateSelection();
			}

			public void focusLost(FocusEvent arg0) {
				updateSelection();
			}
		});
		
		txtBlastFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateSelection();
			}
		});
		
		txtBlastFile.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				updateBlastFilterName();
			}
		});
		
		lblParams = new JLabel("BLAST Parameters");

		String blastParms = (BlastArgs.getBlastxOpDefaults());
		txtParams = new JTextField(30);
		txtParams.setText(blastParms);
		txtParams.setMaximumSize(txtParams.getPreferredSize());
		txtParams.setMinimumSize(txtParams.getPreferredSize());
		
		JPanel row = CompilePanel.getPanel(true);
		row.add(new JLabel("Blast File: "));
		row.add(Box.createHorizontalStrut(10));
		row.add(txtBlastFile);
		
		add(row);
		add(Box.createVerticalStrut(15));
		
		row = CompilePanel.getPanel(true);
		row.add(btnUseBlastOption);
		
		add(row);
		add(Box.createVerticalStrut(10));
				
		row = CompilePanel.getPanel(true);
		row.add(btnGenBlastOption);
		
		add(row);
		add(Box.createVerticalStrut(10));
				
		row = CompilePanel.getPanel(true);
		row.add(Box.createHorizontalStrut(10));
		row.add(lblParams);
		row.add(Box.createHorizontalStrut(5));
		row.add(txtParams);
		
		add(row);
		add(Box.createVerticalStrut(10));

		btnFiltOption = new JCheckBox("Filter Blast File:");
		btnFiltOption.setBackground(Globals.BGCOLOR);
		btnFiltOption.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updatePanel();
			}
		});
		btnFiltOption.setSelected(true);

		row = CompilePanel.getPanel(true);
		row.add(Box.createHorizontalStrut(10));
		row.add(btnFiltOption);
		add(row);
		add(Box.createVerticalStrut(10));
		
		lblBlastFilterHeader = new JLabel("Name:");
		lblBlastFilterName = new JLabel("");
		
		lblFiltID = new JLabel("%Identity:");
		txtFiltID = new JTextField(3);
		txtFiltID.setText(Globals.CompilePanel.BLASTFILTID);
		txtFiltID.setMaximumSize(txtFiltID.getPreferredSize());
		txtFiltID.setMinimumSize(txtFiltID.getPreferredSize());
		txtFiltID.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				updateBlastFilterName();
			}
		});

		lblFiltExtra = new JLabel("Max Nonalign:");
		txtFiltExtra = new JTextField(3);
		txtFiltExtra.setText(Globals.CompilePanel.BLASTFILTEX);
		txtFiltExtra.setMaximumSize(txtFiltExtra.getPreferredSize());
		txtFiltExtra.setMinimumSize(txtFiltExtra.getPreferredSize());
		txtFiltExtra.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				updateBlastFilterName();
			}
		});

		row = CompilePanel.getPanel(true);
		row.add(Box.createHorizontalStrut(10));
		row.add(lblBlastFilterHeader);
		row.add(Box.createHorizontalStrut(HEADER_WIDTH - lblBlastFilterHeader.getPreferredSize().width));
		row.add(lblBlastFilterName);
		add(row);
		add(Box.createVerticalStrut(5));
		row = CompilePanel.getPanel(true);
		row.add(Box.createHorizontalStrut(10));
		row.add(lblFiltID);
		row.add(Box.createHorizontalStrut(HEADER_WIDTH - lblFiltID.getPreferredSize().width));
		row.add(txtFiltID);
		
		add(row);
		
		row = CompilePanel.getPanel(true);
		row.add(Box.createHorizontalStrut(10));
		row.add(lblFiltExtra);
		row.add(Box.createHorizontalStrut(HEADER_WIDTH - lblFiltExtra.getPreferredSize().width));
		row.add(txtFiltExtra);		
		add(row);
		add(Box.createVerticalStrut(40));	
		
		btnKeep = new JButton("Keep");
		btnKeep.setBackground(Globals.BGCOLOR);
		btnKeep.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (btnUseBlastOption.isSelected()) {
					String blastPath = txtBlastFile.getText().trim();
					File f = new File(blastPath);
					if (!f.isFile())
					{
						System.err.println("Error:Specified blast file does not exist");
						JOptionPane.showMessageDialog(theParentPanel, "Error:Specified blast file does not exist",
								"Error", JOptionPane.PLAIN_MESSAGE);
						return;
					}
				}
				if(txtFiltID.getText().trim().length() == 0) {
					System.err.println("Error: %Identity value empty");
					JOptionPane.showMessageDialog(theParentPanel, "Error: %Identity value empty",
							"Error", JOptionPane.PLAIN_MESSAGE);

					return;
				}
				if(txtFiltExtra.getText().trim().length() == 0) {
					System.err.println("Error: Max nonalign value empty");
					JOptionPane.showMessageDialog(theParentPanel, "Error: Max nonalign value empty",
							"Error", JOptionPane.PLAIN_MESSAGE);

					return;
				}
				
				try {
					Integer.parseInt(txtFiltID.getText());
				} catch(Exception e) {
					System.err.println("Error: %Identity value invalid");
					JOptionPane.showMessageDialog(theParentPanel, "Error: %Identity value invalid",
							"Error", JOptionPane.PLAIN_MESSAGE);

					return;
				}
					
				try {
					Integer.parseInt(txtFiltExtra.getText());
				} catch(Exception e) {
					System.err.println("Error: Max nonalign value invalid");
					JOptionPane.showMessageDialog(theParentPanel, "Error: Max nonalign value invalid",
							"Error", JOptionPane.PLAIN_MESSAGE);

					return;
				}
					
				theParentPanel.setBlastSummary();
				theParentPanel.getParentPanel().saveCPAVE();
				theParentPanel.setMainPanelVisible(true);
				setVisible(false);
			}
		});
		
		btnDiscard = new JButton("Discard");
		btnDiscard.setBackground(Globals.BGCOLOR);
		btnDiscard.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				theParentPanel.resetValsOnDiscard();
				theParentPanel.setBlastSummary();
				theParentPanel.setMainPanelVisible(true);
				setVisible(false);
			}
		});
		
		btnReset = new JButton("Reset to default");
		btnReset.setBackground(Globals.BGCOLOR);
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtBlastFile.setText(theParentPanel.getDefaultBlastTextEntry());
				txtParams.setText(BlastArgs.getBlastxOpDefaults());
				btnUseBlastOption.setSelected(true);
				if((new File(txtBlastFile.getText())).exists())
					btnUseBlastOption.setSelected(true);
				else
					btnGenBlastOption.setSelected(true);
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBackground(Globals.HELPCOLOR);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserPrompt.displayHTMLResourceHelp(theParentPanel.getParentPanel().getParentFrame(), "Edit Blast Settings", "html/runMultiTCW/EditBlastPanel.html");
			}
		});
	
		buttonPanel = CompilePanel.getPanel(true);
		buttonPanel.add(Box.createHorizontalGlue());
		buttonPanel.add(btnKeep);
		buttonPanel.add(Box.createHorizontalStrut(5));
		buttonPanel.add(btnDiscard);
		buttonPanel.add(Box.createHorizontalStrut(5));
		buttonPanel.add(btnReset);
		buttonPanel.add(Box.createHorizontalStrut(5));
		buttonPanel.add(btnHelp);
		buttonPanel.add(Box.createHorizontalGlue());
		add(buttonPanel);
		add(Box.createVerticalStrut(10));
		
		setMaximumSize(getPreferredSize());
		setMinimumSize(getPreferredSize());
		
		updatePanel();
	}
	
	public void updateSelection() {
		updateBlastFilterName();
	}
	
	public void updatePanel() {
		boolean enabled = !btnUseBlastOption.isSelected();
		
		lblParams.setEnabled(enabled);
		txtParams.setEnabled(enabled);
		
		enabled = btnFiltOption.isSelected();
		
		lblFiltID.setEnabled(enabled);
		lblFiltExtra.setEnabled(enabled);
		txtFiltID.setEnabled(enabled);
		txtFiltExtra.setEnabled(enabled);
		lblBlastFilterHeader.setEnabled(enabled);
		lblBlastFilterName.setEnabled(enabled);
	}
	
	private void updateBlastFilterName() {
		lblBlastFilterName.setText(txtBlastFile.getText() + "." + txtFiltID.getText() + "." + txtFiltExtra.getText() + Globals.CompilePanel.BLASTFILTSUF);
	}
	
	public boolean useBlast() { return btnUseBlastOption.isSelected(); }
	public void setUseBlast(boolean useBlast) { 
		btnUseBlastOption.setSelected(useBlast);
		btnGenBlastOption.setSelected(!useBlast);
		updatePanel();
	}
	public boolean filterBlast() { return btnFiltOption.isSelected(); }

	public void fillEmptyFieldsWithDefaults() {
		if(txtBlastFile.getText().trim().length() == 0)
		{
			txtBlastFile.setText(theParentPanel.getDefaultBlastTextEntry());
		}
		if(txtParams.getText().trim().length() == 0)
		{
			txtParams.setText(BlastArgs.getBlastxOpDefaults());
		}
		String blastFileName = txtBlastFile.getText().trim();

		if((new File(blastFileName)).exists()) {
			btnUseBlastOption.setSelected(true); 
			btnGenBlastOption.setSelected(false);
		}
		else {
			btnUseBlastOption.setSelected(false); 
			btnGenBlastOption.setSelected(true);
		}
	}
	
	public String getBlastFileName() {
		String contents = txtBlastFile.getText().trim();
		return contents; 
	}
	public String getFilteredBlastFileName() {	
		String contents = lblBlastFilterName.getText().trim();
		return contents; 
	}	
	public void setFilteredBlastFileName(String file) 
	{ 
		if(file.startsWith(theParentPanel.getDefaultBlastDirFullPath()))
			file = file.replace(theParentPanel.getDefaultBlastDirFullPath(),theParentPanel.getDefaultBlastDirRelPath() );
		lblBlastFilterName.setText(file); 
	}

	public String getParams() { 
		if(txtParams.getText().trim().length() == 0)
			txtParams.setText(BlastArgs.getBlastxOpDefaults());			
		return txtParams.getText(); 
	}
	public String getFiltID() { 
		if(txtFiltID.getText().trim().length() == 0)
			txtFiltID.setText(Globals.CompilePanel.BLASTFILTID);			
		return txtFiltID.getText(); 
	}	
	public void setFiltID(String idPercent) { txtFiltID.setText(idPercent); }
	public String getFiltExtra() { 
		if(txtFiltExtra.getText().trim().length() == 0)
			txtFiltExtra.setText(Globals.CompilePanel.BLASTFILTEX);			
		return txtFiltExtra.getText(); 
	}	
	public void setFiltExtra(String filtExtra) { txtFiltExtra.setText(filtExtra); }
	public void setBlastFileName(String file) { 
		if(file.startsWith(theParentPanel.getDefaultBlastDirFullPath()))
			file = file.replace(theParentPanel.getDefaultBlastDirFullPath(),
					theParentPanel.getDefaultBlastDirRelPath() );
		txtBlastFile.setText(file);
		updateSelection();
		updatePanel();
	}
	public void setUseFilter(boolean use) { btnFiltOption.setSelected(use); }
	public void setParams(String params) { txtParams.setText(params); }
	
	private BlastPanel theParentPanel = null;
	private JPanel buttonPanel = null;
	private FileSelectTextField txtBlastFile = null;
	private JRadioButton btnUseBlastOption = null, btnGenBlastOption = null;
	private JCheckBox	btnFiltOption = null;
	private JLabel lblBlastFilterHeader = null;
	private JLabel lblBlastFilterName = null;
	private JLabel lblParams = null;
	private JLabel lblFiltID = null;
	private JLabel lblFiltExtra = null;
	private JTextField txtParams = null;
	private JTextField txtFiltID = null;
	private JTextField txtFiltExtra = null;
	private JButton btnKeep = null, btnDiscard = null, btnReset = null, btnHelp = null;
}
